import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import user from './modules/user'
import product from './modules/product'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    msg:'welcome to vuex',
    name:'tom'
  },
  getters: {
    reverseMsg(state){
      return state.msg.toUpperCase().split(' ').reverse().join('_')
    }
  },
  mutations: {
    setMsg(state,newValue){
      state.msg = newValue
    }
  },
  actions: {
    async setMsgAsync(context,uid){
      const {data:res} = await axios.get(`http://tangxiaoyang.vip:8881/ums/user/findById?id=${uid}`)
      console.log(res)
      if(res.status === 1){
        context.commit('setMsg',res.data.name) // 提交mutations
      }
    }
  },
  modules: { // 模块化
    user,
    product
  }
})
